from .SumaMain import *
from .SumaHTTP import *
from .SumaLog import *

class SumaFile:
    def __init__(self):
        self.creater_name = "SumaRoder"
        self.creater_qq = "1493813167"
        self.function_name = "SumaFile"
        self.function_desc = "This is a File manager"
        self.function_version = "2.7.8"
    def mkdir(self,path):
        try:
            if not os.path.exists(os.getcwd()+"/"+path):
                os.makedirs(os.getcwd()+"/"+path)
                return True
            else:
                return False
        except:
            return None
    def saveUrlImg(self,url,savePath,fileName=None,method="get",headers={},postParam=None):
        if fileName is None:
            timestamp = time.time()
            local_time = time.localtime(timestamp)
            year = local_time.tm_year
            month = local_time.tm_mon
            day = local_time.tm_mday
            hour = local_time.tm_hour
            minute = local_time.tm_min
            second = local_time.tm_sec
            key = secrets.token_hex(12)
            fileName = f"{year}{month:02d}{day:02d}{hour:02d}{minute:02d}{second:02d}-{key}.jpg"
        if savePath.endswith("/"):
            savePath = savePath[0:-1:1]
        SumaHTTP.OpenHTTPConnection(url,headers=headers,method=method,param=postParam)
        response = SumaHTTP.SendHTTPConnection(responseType="content")
        if response:
            with open(savePath+"/"+fileName, 'wb') as f:
                try:
                    f.write(response)
                    f.close()
                    return fileName+" saved."
                except Exception as e:
                    SumaLog.log("",type="error",e=e)
                    traceback.print_exc(file=sys.stderr)
                    f.close()
                    return False
        else:
            return None
    def writeFile(self,savePath,text,fileName=None,writeType="w"):
        if fileName is None:
            timestamp = time.time()
            local_time = time.localtime(timestamp)
            year = local_time.tm_year
            month = local_time.tm_mon
            day = local_time.tm_mday
            hour = local_time.tm_hour
            minute = local_time.tm_min
            second = local_time.tm_sec
            key = secrets.token_hex(12)
            fileName = f"{year}{month:02d}{day:02d}{hour:02d}{minute:02d}{second:02d}-{key}.jpg"
        if savePath.endswith("/"):
            savePath = savePath[0:-1:1]
        if text:
            with open(savePath+"/"+fileName, writeType) as f:
                try:
                    f.write(text)
                    f.close()
                    return fileName+" saved."
                except Exception as e:
                    SumaLog.log("",type="error",e=e)
                    traceback.print_exc(file=sys.stderr)
                    f.close()
                    return False
        else:
            return None
    def readFile(self,filePath,fileName):
        if savePath.endswith("/"):
            savePath = savePath[0:-1:1]
        with open(filePath+"/"+fileName, 'r') as f:
            try:
                text = f.read()
                f.close()
                return text
            except Exception as e:
                SumaLog.log("",type="error",e=e)
                traceback.print_exc(file=sys.stderr)
                f.close()
                return False
SumaFile = SumaFile()
